package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.controller.*;
import com.neudu.yiyang.pojo.CustomerPreference;
import com.neudu.yiyang.service.CustomerPreferenceService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CustomerPreferenceController {
    @Autowired
    private CustomerPreferenceService customerPreferenceService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_customerpreferences")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<CustomerPreference> list = customerPreferenceService.list();
            PageInfo<CustomerPreference> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_customerpreferences_by")
    public String getByCondition(@RequestBody CustomerPreferenceQuery query) {
        try {
            PageHelper.startPage(query.getPageInfo().getPageNum(), query.getPageInfo().getPageSize());

            LambdaQueryWrapper<CustomerPreference> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(query.getCustomerPreference().getId()), CustomerPreference::getId, query.getCustomerPreference().getId())
                    .eq(!ObjectUtils.isEmpty(query.getCustomerPreference().getCustomerId()), CustomerPreference::getCustomerId, query.getCustomerPreference().getCustomerId())
                    .like(!ObjectUtils.isEmpty(query.getCustomerPreference().getPreferences()), CustomerPreference::getPreferences, query.getCustomerPreference().getPreferences())
                    .like(!ObjectUtils.isEmpty(query.getCustomerPreference().getAttention()), CustomerPreference::getAttention, query.getCustomerPreference().getAttention())
                    .like(!ObjectUtils.isEmpty(query.getCustomerPreference().getRemark()), CustomerPreference::getRemark, query.getCustomerPreference().getRemark())
                    .eq(!ObjectUtils.isEmpty(query.getCustomerPreference().getIsDeleted()), CustomerPreference::getIsDeleted, query.getCustomerPreference().getIsDeleted());
            List<CustomerPreference> customerPreferences = customerPreferenceService.list(queryWrapper);
            PageInfo<CustomerPreference> pageInfo1 = new PageInfo<>(customerPreferences);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("add_one_customerpreference")
    public String addOne(@RequestBody CustomerPreference customerPreference) {
        try {
            // 不要手动设置 ID，让数据库自动生成
            customerPreference.setId(null);
            customerPreferenceService.save(customerPreference);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getcustomerpreferenceby_id")
    public String getById(@RequestParam("id") Integer id) {
        try {
            CustomerPreference customerPreference = customerPreferenceService.getById(id);
            return jsonReturn.returnSuccess(customerPreference);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifycustomerpreferenceby_id")
    public String modifyById(@RequestBody CustomerPreference customerPreference) {
        try {
            customerPreferenceService.updateById(customerPreference);
            return jsonReturn.returnSuccess(customerPreference);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removecustomerpreference_by_id")
    public String removeById(@RequestParam("id") Integer id) {
        try {
            if (id == null) {
                throw new IllegalArgumentException("ID cannot be null");
            }
            customerPreferenceService.removeById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

// 新增查询请求对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class CustomerPreferenceQuery {
    private CustomerPreference customerPreference;
    private PageInfo pageInfo;
}
}